<?php

namespace App\Exports;


use App\Models\ArchiveManageVolume;
use App\Models\AccountArchive;
use App\Models\LedgerTender;
use App\Models\Project;
use App\Models\Storeroom;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithCustomStartCell;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithTitle;

class AccountArchiveExport implements FromCollection, WithColumnWidths, WithStyles, WithCustomStartCell, WithTitle
{
    use Exportable;

    protected $totalRows;
    protected $projectId;
    protected $type;
    protected $keyword;
    protected $selectIds;
    protected $sheetName;
    protected $sheetTitle;

    public function __construct($projectId, $keyword, $selectIds)
    {
        $projectName = Project::query()->where('id', $projectId)->value('title');

        $this->projectId  = $projectId;
        $this->keyword    = $keyword;
        $this->selectIds  = $selectIds;
        $this->sheetName  = '会计档案';
        $this->sheetTitle = '会计档案案卷目录'; //$projectName . $this->sheetName;
    }

    public function collection()
    {
        $thisQuery = AccountArchive::query()
            ->where('project_id', $this->projectId);
        if ($this->selectIds) {
            $thisQuery->whereIn('id', to_array($this->selectIds));
        }
        if ($this->keyword) {
            $likeKeyword = $this->keyword;
            $thisQuery->where('serial_no', 'like', $likeKeyword)->orWhere('deal_number', 'like', $likeKeyword)->orWhere('title', 'like', $likeKeyword);
        }
        $list = $thisQuery->orderBy('serial_no')->get();
        if ($list->isEmpty()) return [];
        $list = $list->toArray();
        $r_list = [];
        foreach ($list as $val) {
            $tmp                   = [];
            $tmp['serial_no']     = $val['serial_no'];
            $tmp['deal_number']    = $val['deal_number'];
            $tmp['type']           = $val['type'];
            $tmp['title']          = $val['title'];
            $tmp['start_end_date'] = $val['start_end_date'];
            $tmp['store_period']   = $val['store_period'];
            $tmp['catalog_count']  = $val['catalog_count'];
            $tmp['store_position'] = $val['store_position'];
            $tmp['remark']         = $val['remark'];
            $tmp['box_size']       = $val['box_size'];
            $r_list[]              = $tmp;
        }
        $this->totalRows = count($r_list) + 2;

        return collect($r_list);
    }

    /**
     * 数组开头插入空行
     *
     * @param $list
     * @param int $rowCount
     * @return string[]
     * @author: lxk
     * @date: 2025-01-10 16:05
     */
    public static function addEmptyRows($list, int $rowCount = 1) : array
    {
        $row = [
            'serial_no'      => '',
            'deal_number'    => '',
            'type'           => '',
            'title'          => '',
            'start_end_date' => '',
            'store_period'   => '',
            'catalog_count'  => '',
            'store_position' => '',
            'remark'         => '',
            'box_size'       => '',
        ];
        for ($i=0; $i < $rowCount; $i++) {
            array_unshift($list, $row);
        }

        return $row;
    }

    /**
     * 从哪个单元格开始渲染数据，数据集合如 $list
     * @return string
     */
    public function startCell(): string
    {
        return 'A3';
    }

    /**
     * 设置列宽
     * @return array
     */
    public function columnWidths(): array
    {
        return [
            'A' => 15, 'B' => 20, 'C' => 20, 'D' => 60, 'E' => 30, 'F' => 15, 'G' => 15, 'H' => 15, 'I' => 15, 'J' => 15
        ];
    }

    /**
     * 样式设置
     * @param Worksheet $sheet
     * @throws PhpSpreadsheetException
     */
    public function styles(Worksheet $sheet)
    {
        $sheet->getDefaultRowDimension()->setRowHeight(36); //设置行高(全部行高)
        $sheet->getStyle('A1:J' . $this->totalRows)->getFont()->setName('宋体'); //宋体
        $sheet->getStyle('A1:J' . $this->totalRows)->getFont()->setSize(12); //大小
        $sheet->getStyle('A1')->getFont()->setSize(22); //大小
        $sheet->getStyle('A1:J' . $this->totalRows)->getAlignment()->setWrapText(true); //自动换行

        $sheet->mergeCells('A1:J1');
        $sheet->setCellValue('A1', $this->sheetTitle);
        $sheet->getStyle('A1:J2')->getFont()->setBold(true);

        /**
         * 属性名设置
         */
        $sheet->setCellValue('A2', '案卷号');
        $sheet->setCellValue('B2', '会计处理号');
        $sheet->setCellValue('C2', '类别');
        $sheet->setCellValue('D2', '案卷题名');
        $sheet->setCellValue('E2', '起止日期');
        $sheet->setCellValue('F2', '保管期限');
        $sheet->setCellValue('G2', '卷内数量');
        $sheet->setCellValue('H2', '存放位置');
        $sheet->setCellValue('I2', '备注');
        $sheet->setCellValue('J2', '盒子尺寸');

        /**
         * 顶部案卷目录样式设置
         */
        $sheet->getStyle('A1:J' . $this->totalRows)->getAlignment()->setVertical('center');//垂直居中
        $sheet->getStyle('A1:J' . $this->totalRows)->getAlignment()->setHorizontal('center');//水平居左

        $sheet->getStyle('D3:E' . $this->totalRows)->getAlignment()->setHorizontal('left');//水平居左

        /**
         * 设置边框
         */
        $styleArray = [
            'borders' => [
                'allBorders' => [
                    'borderStyle' => Border::BORDER_THIN //细边框
                ]
            ]
        ];
        $sheet->getStyle('A1:J' . $this->totalRows)->applyFromArray($styleArray);
    }

    /**
     * sheet名称
     * @return string
     */
    public function title(): string
    {
        return $this->sheetName;
    }


}
